<?
require 'global.php';
$sales_order=new DB(Tsales_order,$conn);
$sales_order_detail=new DB(Tsales_order_detail,$conn);

// 在线支付方式
// 1支付宝/2财付通/3payapl/4网银在线/5货到付款/6银行转账

// 前往支付页面
$order = $_POST;
$payment_method = 0; if(isset($order['payment_method'])) $payment_method = $order['payment_method'];
$arr = array('payment_method_id'=>$payment_method);
if(isset($_POST['wallet_amount'])) $arr['wallet_amount'] = $_POST['wallet_amount'];
if(isset($_POST['total_price'])) $arr['total_price'] = $_POST['total_price'];


if(! $sales_order->update($arr, 'ref_no', $order['ref_no'])){
    die('System error.');
}
if($_POST['wallet_amount']){
    $customer_info = $customer->get_row(decrypt($_COOKIE['customer_id']));
    if($_POST['wallet_amount'] > $customer_info['usable_balance']) die('Illegal operation.');
    $customer_arr['usable_balance'] = $customer_info['usable_balance'] - $_POST['wallet_amount'];
    $customer_arr['all_balance'] = $customer_info['all_balance'] - $_POST['wallet_amount'];

    $sales_order_id = $sales_order->get_id('ref_no', $_POST['ref_no']);
    $sales_order_info = $sales_order_detail->get_rows(1024, 'sales_order_id', $sales_order_id);
    $str_var='';
    foreach($sales_order_info as $row){
        $tem_product=$product->get_row($row['product_id']);
        $tem_name=$tem_product['name_'.$lang];
        $tem_qty=$row['qty'];
        $str_var .= $tem_name.' ';
    // variation
        $tem_var=$product_variation->get_row($row['var']);
        if($tem_var['size']) $str_var.=$option_value[$tem_var['size']].' ';
        if($tem_var['color']) $str_var.=$option_value[$tem_var['color']].' ';
        if($tem_var['other1']) $str_var.=$option_value[$tem_var['other1']].' ';
        if($tem_var['other2']) $str_var.=$option_value[$tem_var['other2']].' ';
        $str_var .=', ';
    }
    $str_var = substr($str_var, 0, -2);

    $customer->update($customer_arr, 'id', decrypt($_COOKIE['customer_id']));
    $recode_arr = array(
        'template_id' => 3,
        'value' => $_POST['ref_no'].'|'.$str_var.'|'.($_POST['total_price'] + $_POST['wallet_amount']).'|'.$_POST['wallet_amount'].'|'.$customer_info['email'],
        'type' => 2,
        'customer_id' => decrypt($_COOKIE['customer_id']),
        'user_id' => 0,
        'create_time' => date('Y-m-d H:i:s', time()),
        'recode_name' => 'sales_order_id',
        'recode_value' => $_POST['ref_no']
    );
    $recode->insert($recode_arr);
}


// payment info 支付信息
$ref_no = $order['ref_no'];
$payment_method_id=$order['payment_method'];
$subject=WEB_ORDER.' - '.$ref_no.' - '.$config['SITE_COMPANY_'.LANG];
$body=$subject;
$pay_price = $order['total_price'];
//if($order['with_deposit']) $pay_price = $order['deposit'];

if(!$_POST['total_price'] || $_POST['total_price'] == 0){
    $message=array(
        'tip' => '钱包支付,支付金额：'.$_POST['wallet_amount'],
    	'links'=>array(array('name'=>WEB_MY_ORDER,'url'=>'myorder.html'),array('name'=>WEB_BACK_HOME,'url'=>'./')),
        'time' => 6.2,
        'url'=> 'myorder.html'
    );
    $sales_order_arr = array('order_status'=>2, 'payment_method_id'=>7);
    $sales_order->update($sales_order_arr,'ref_no', $_POST['ref_no']);
    require VIEW.'message.php';
    exit;
}
// 支付宝 Alipay 
if($payment_method_id==1){
	$action='api_alipay.php';
	require VIEW.'api_checkout.php';
	exit;
}
// Paypal
if($payment_method_id==3||$payment_method_id==7){
	$action='api_paypal.php';
	require VIEW.'api_checkout.php';
	exit;
}
// bank transfer/COD 货到付款/银行转账
if($payment_method_id==5){
	$message=array(
	'tip'=>WEB_ORDER_SUBMIT_COD,
	'links'=>array(array('name'=>WEB_MY_ORDER,'url'=>'myorder.html'),array('name'=>WEB_BACK_HOME,'url'=>'./')),
	'time'=>6.2,
	'url'=>'myorder.html',
	);
	require VIEW.'message.php';
	exit;
}
// 银行转账 
if($payment_method_id==6){
	$message=array(
	'tip'=>WEB_ORDER_SUBMIT_BANK,
	'links'=>array(array('name'=>WEB_MY_ORDER,'url'=>'myorder.html'),array('name'=>WEB_BACK_HOME,'url'=>'./')),
	'time'=>6.2,
	'url'=>'myorder.html',
	);
	require VIEW.'message.php';
	exit;
}

// SEO 设置 seo tags
$title=WEB_ORDER_SUBMIT.' - '.$config['SITE_COMPANY_'.LANG];
$kwd=$title;
$dpn=$kwd;

// view 加载视图
//require VIEW.'order.php';
// end of file
